package com.ruoyi.web.controller.price;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.price.domain.TbFormula;
import com.ruoyi.price.domain.dto.TbFormulaDTO;
import com.ruoyi.price.service.ITbFormulaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 公式信息Controller
 * 
 * @author ruoyi
 * @date 2023-03-06
 */
@Controller
@RequestMapping("/price/formula")
public class TbFormulaController extends BaseController
{
    private String prefix = "price/formula";

    @Autowired
    private ITbFormulaService tbFormulaService;

    @RequiresPermissions("price:formula:view")
    @GetMapping()
    public String formula()
    {
        return prefix + "/formula";
    }

    /**
     * 查询公式信息列表
     */
    @RequiresPermissions("price:formula:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbFormula tbFormula)
    {
        startPage();
        List<TbFormula> list = tbFormulaService.listByCondition(tbFormula,getSysUser());
        return getDataTable(list);
    }

    /**
     * 导出公式信息列表
     */
    @RequiresPermissions("price:formula:export")
    @Log(title = "公式信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbFormula tbFormula)
    {
        List<TbFormula> list = tbFormulaService.listByCondition(tbFormula,getSysUser());
        ExcelUtil<TbFormula> util = new ExcelUtil<TbFormula>(TbFormula.class);
        return util.exportExcel(list, "公式信息数据");
    }

    /**
     * 新增公式信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Integer> formulaCountList = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30).collect(Collectors.toList());
        mmap.put("formulaCountList",formulaCountList);
        return prefix + "/add";
    }

    /**
     * 新增保存公式信息
     */
    @RequiresPermissions("price:formula:add")
    @Log(title = "公式信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated @RequestBody TbFormulaDTO tbFormula)
    {
        return toAjax(tbFormulaService.saveFormula(tbFormula));
    }

    /**
     * 修改公式信息
     */
    @RequiresPermissions("price:formula:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbFormulaDTO formula = tbFormulaService.getFormulaById(id);
        mmap.put("formula", formula);
        List<Integer> formulaCountList = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).collect(Collectors.toList());
        mmap.put("formulaCountList",formulaCountList);
        return prefix + "/edit";
    }

    /**
     * 修改保存公式信息
     */
    @RequiresPermissions("price:formula:edit")
    @Log(title = "公式信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated @RequestBody TbFormulaDTO formulaDTO)
    {
        return toAjax(tbFormulaService.updateFormulaById(formulaDTO));
    }

    /**
     * 删除公式信息
     */
    @RequiresPermissions("price:formula:remove")
    @Log(title = "公式信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbFormulaService.removeFormulaByIds(Arrays.asList(ids)));
    }

}
